package com.example.myanimation;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import butterknife.BindView;
import butterknife.ButterKnife;

public class XmlActivity extends AppCompatActivity {


    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml);
        ButterKnife.bind(this);
        initEvent();
    }

    private void initEvent() {

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1:
                        animator();
                        break;
                    case 2:
                        objectAnimator();
                        break;
                    case 3:
                        animSet();
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });
    }

    private void animSet() {

        AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animatorset);
        set.setTarget(bt2);
        set.start();
    }


    private void objectAnimator() {
        ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.object_animator);
        objectAnimator.setTarget(bt1);
        objectAnimator.start();
    }

    private void animator() {
        ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.value_animator);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                /**
                 * 这四个值是相对于父控件而言的。例如传入的是（10, 10, 100, 100），
                 * 则该View在距离父控件的左上角位置(10, 10)处显示，
                 * 显示的大小是宽高是90(参数r,b是相对左上角的)，这有点像绝对布局。
                 */
                bt1.layout(value,value,value+bt1.getWidth(),value+bt1.getHeight());
            }
        });
        animator.start();
    }
}
